<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
	<jsp:include page="/include.jsp"/>
	<script type="text/javascript">
	 $(document).ready(function(){
		 doQuery();
	 });
	//分页查询
	function doQuery()
	{
		$('#framework_role_table').datagrid({
			url :'${pageContext.request.contextPath}/systemManagerAction!getEtlRoleData.action',
			queryParams:serializeObject($('#framework_role_table_form')),
			fit:true,
			border:false,
			fitColumns:true,
			pagination:true,
			striped:true,
			pageList:[10,15,20],
			rownumbers:true,
			singleSelect:false,
			pagePosition:'bottom',
			autoRowHeight:true,
			rownumbers:false,
			sortName:'roleid',
			columns:[[  {field:'roleid',checkbox:true,hidden:true},
						{field:'rolename',title:'角色名称',width:80,align:'left',sortable:true},
						{field:'limitModule',title:'已授权功能模块',width:140,align:'left',formatter:function(value,rowData,rowIndex){
							if(""!=rowData.limitModule){
								return '<span>'+rowData.limitModule+'</span>'; 
							}else{
								return '';
							}
							
						}},
						{field:'descinfo',title:'角色描述',width:140,align:'left',formatter:function(value,rowData,rowIndex){
							if(rowData.descinfo!=undefined){
 								return '<div style="width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="'+rowData.descinfo+'">'+rowData.descinfo+'</div>';
 							}
						}},
						{field:'dsafd',title:'操作',width:80,align:'left',formatter:function(value,rowData,rowIndex){
							if(rowData.roleid=="10001"){
								return '<span  style="cursor:pointer;margin-right:15px;" onclick="doAssignPermission(\''+rowData.roleid+'\')"><img  src="${pageContext.request.contextPath}/images/icons/modify_or_edit.png" title="分配权限" alt="分配权限" style="margin-top:2px;"/>分配权限</span>'
                                    +'<span  style="cursor:pointer;margin-right:15px;" onclick="doEdit(\''+rowData.roleid+'\')"><img  src="${pageContext.request.contextPath}/images/icons/modify_or_edit.png" title="修改" alt="修改" style="margin-top:2px;"/>修改</span>'
								      +'<span  style="cursor:pointer;" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
							}else{
								return  '<span  style="cursor:pointer;margin-right:15px;" onclick="doAssignPermission(\''+rowData.roleid+'\')"><img  src="${pageContext.request.contextPath}/images/icons/modify_or_edit.png" title="分配权限" alt="分配权限" style="margin-top:2px;"/>分配权限</span>'
                                        +'<span  style="cursor:pointer;margin-right:15px;" onclick="doEdit(\''+rowData.roleid+'\')"><img  src="${pageContext.request.contextPath}/images/icons/modify_or_edit.png" title="修改" alt="修改" style="margin-top:2px;"/>修改</span>'
                                        +'<span  style="cursor:pointer" onclick="doDelete(\''+rowData.roleid+'\')"><img  src="${pageContext.request.contextPath}/images/icons/delete.png" title="删除" alt="删除" style="margin-top:2px;"/>删除&nbsp;&nbsp;</span>';
							}
						}}
					]],
			toolbar:'#framework_role_table_toobar'
		});
	}
	
	//修改用户角色
	function doEdit(roleid){
		window.top.$('<div/>').dialog({
			href:'${pageContext.request.contextPath}/systemManagerAction!modifyLimitRoleResult.action?roleid='+roleid,
			modal : true,
			width:450,
			height:400,
			title : '修改角色',
			buttons : [ {
				text : '修改',
				iconCls : '',
				handler : function() {
					var d=window.top.$(this).closest('.window-body');
					if (window.top.$("#framework_modify_moudle_dirstrmoudleform").form("validate"))
					{
						var str=window.top.getLimitMoudle();//获取已授权的功能模块
						if(""!=str && str.length>0)
						{
							var roleid = window.top.$("#ramework_modify_moudle_roleid").val();
							var rolename = window.top.$("#framework_rolename").val();
							$.ajax({
								 url : '${pageContext.request.contextPath}/systemManagerAction!validRoleName.action',
								 data:{rolename:rolename, roleid:roleid,operateType:'edit'},
								 dataType :'json',
								 cache : true,
								 beforeSend:function(){
									 window.top.$.messager.progress({title : '提示',text : '正在保存，请稍候 ....'});
								 },
								 success : function(r){ 
									 if (r.returnFlag) {
										 window.top.$.messager.show({title : '提示',msg : '该角色名称已存在'});
										 window.top.$.messager.progress('close'); 
								     }else{
											window.top.$('#framework_modify_moudle_dirstrmoudleform').form('submit', {
												url :'${pageContext.request.contextPath}/systemManagerAction!modifyRoleData.action?moudleid='+str,
												success:function(result){
													try {
														var r=$.parseJSON(result);
														//显示提示信息
														$.messager.show({
															title:'提示',
															msg:r.returnMsg
														});
														$('#framework_role_table').datagrid('reload');
														//关闭新增窗口
														d.dialog('destroy'); 	
														window.top.$.messager.progress('close'); 
													} catch (e) {
														$.messager.alert('提示', result);
														window.top.$.messager.progress('close'); 
													}
												}
											});
								     }
								 }
							 });
						}
						else
						{
							window.top.$.messager.show({
								title:'操作提示',
								msg:'该角色未授权功能模块，请授权！'
							});
						}
					}
				}
			}],
			onClose : function() {
				window.top.$(this).dialog('destroy');
				$('#framework_role_table').datagrid('reload');
			}
		});
	}
	//新增用户角色
	function doAdd(){
		window.top.$('<div/>').dialog({
			href:'${pageContext.request.contextPath}/systemManagerAction!newLimitRoleResult.action',
			modal : true,
			width:450,
			height:400,
			title : '新增角色',
			buttons : [ {
				text : '保存',
				iconCls : '',
				handler : function() {
					var d=window.top.$(this).closest('.window-body');
					if (window.top.$("#framework_new_moudle_dirstrmoudleform").form("validate"))
					{
						var moudleStr=window.top.getLimitMoudle();//获取已授权的功能模块
						if(""!=moudleStr && moudleStr.length>0){
							var rolename=window.top.$("#framework_rolename").val();
							$.ajax({
								 url : '${pageContext.request.contextPath}/systemManagerAction!validRoleName.action',
								 data:{"rolename":rolename},
								 dataType :'json',
								 cache : true,
								 beforeSend:function(){
									 window.top.$.messager.progress({title : '提示',text : '正在保存，请稍候 ....'});
								 },
								 success : function(r){ 
									 if (r.returnFlag) {
										 window.top.$.messager.show({title : '提示',msg : '该角色名称已存在'});
										 window.top.$.messager.progress('close');
								     }else{
								    	 window.top.$('#framework_new_moudle_dirstrmoudleform').form('submit', {
												url :'${pageContext.request.contextPath}/systemManagerAction!addRoleData.action?moudleid='+moudleStr,
												success:function(result){
													try {
														var r=$.parseJSON(result);
														//显示提示信息
														$.messager.show({
															title:'提示',
															msg:r.returnMsg
														});
														$('#framework_role_table').datagrid('reload');
														//关闭新增窗口
														d.dialog('destroy'); 	
														window.top.$.messager.progress('close'); 
													} catch (e) {
														$.messager.alert('提示', result);
														window.top.$.messager.progress('close'); 
													}
												}
											});
								     }
								 }
							 });
						}else{
							window.top.$.messager.show({
								title:'操作提示',
								msg:'该角色未授权功能模块，请授权！'
							});
						}
					}
				}
			}],
			onClose : function() {
				window.top.$(this).dialog('destroy');
				$('#framework_role_table').datagrid('reload');
			}
		});
	}
	//新增用户角色
	function doAssignPermission(roleid){
		window.top.$('<div/>').dialog({
			href:'${pageContext.request.contextPath}/systemManagerAction!doAssignPermissionResult.action?roleid='+roleid,
			modal : true,
			width:450,
			height:400,
			title : '分配权限',
			buttons : [ {
				text : '保存',
				iconCls : '',
				handler : function() {
                    var d=window.top.$(this).closest('.window-body');
                    var selectIds = [];
                    var nodes =  window.top.$('#framework_menu_treegrid1').tree('getChecked');
                    for(var key in nodes){
                        selectIds.push(nodes[key].id);
                    }
                    var nodes =  window.top.$('#framework_menu_treegrid1').tree('getChecked', 'indeterminate');
                    for(var key in nodes){
                        selectIds.push(nodes[key].id);
                    }

                    $.ajax({
                        url: '${pageContext.request.contextPath}/systemManagerAction!assignPermission.action',
                        data: {'roleid' : roleid, 'permissionIds' : selectIds.join(";")},
                        type: "post",
                        cache: false,
                        dataType: 'json',
                        timeout: 30000,
                        beforeSend: function (XMLHttpRequest) {
                        },
                        success: function (data) {
                            $.messager.show({
                                title : '提示',
                                msg : data.returnMsg
                            });
                            if(data.returnFlag){
                                //关闭新增窗口
                                d.dialog('destroy');
                                window.top.$.messager.progress('close');
                            }
                        },
                        error: function (errorThrown) {
                            $.messager.show({
                                title : '提示',
                                msg : '系统异常，请稍后再操作。'
                            });
                        }
                    });
				}
			}],
			onClose : function() {
				window.top.$(this).dialog('destroy');
				$('#framework_role_table').datagrid('reload');
			}
		});
	}
	//单独删除
	function doDelete(roleid){
		 $.ajax({
			 url : '${pageContext.request.contextPath}/systemManagerAction!ValidRoleContainUser.action',
			 data:{"roleid":roleid},
			 dataType :'json',
			 cache : true,
			 success : function(r){ 
				 if (r.returnFlag) {
					 $.messager.show({
							title : '提示',
							msg : r.returnMsg
					 });
				 }else{
					 $.messager.confirm('删除确认', '确定要删除该条记录吗？',function(r){
							if(r){
								$('#framework_role_table_form').form('submit',{
									url:'${pageContext.request.contextPath}/systemManagerAction!deleteBatchRole.action?batchFlag=false&pid='+roleid,
								    success:function(result){
									   try {
											var r = $.parseJSON(result);
											$.messager.show({
												title : '提示',
												msg : r.returnMsg
											});
											if (r.returnFlag) {
												$('#framework_role_table').datagrid('reload');
											}
										} catch (e) {
											$.messager.alert('提示', result);
										}
								    } 
						       });
							}
						});
				 }
				
			 }
	     });
	}
	
	//批量删除
	function doBatchDelete()
	{
		var rows=$('#framework_role_table').datagrid('getChecked')
		if(rows.length<=0){
			$.messager.alert('提示','没有选择任何记录');
		}else{
			$.messager.confirm('删除确认', '确定要删除所选择的记录吗？', function(r){
				if (r){
						$('#framework_role_table_form').form('submit',{
						url:'${pageContext.request.contextPath}/systemManagerAction!deleteBatchRole.action?batchFlag=true',
						success:function(result){
							try {
								var r = $.parseJSON(result);
								$.messager.show({
									title : '提示',
									msg : r.returnMsg
								});
								if (r.returnFlag) {
									$('#framework_role_table').datagrid('reload');
								}
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			});
		}
	}
	</script>
</head>

<body class="easyui-layout">
    <form id="framework_role_table_form" method="post">
		<div data-options="region:'center'" style="border-top:none">
			<table id="framework_role_table"></table>
			<div id="framework_role_table_toobar" class="etl_panel_title" >
				<a href="javascript:void(0);" onclick="doAdd()" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" style="float:left">新增角色</a>
				<div class="datagrid-btn-separator"></div>
				<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'',plain:true"></a>
				<!--  <a href="javascript:void(0);" onclick="doBatchDelete()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">批量删除</a>-->
			</div>
		</div>
    </form>
</body>
</html>
